﻿
using EMS.system.Entityies.MasterData.SupplierManagementModel;
using EMS.system.IEntityAppService.MasterData.SupplierManagementIService;
using EMS.system.IEntityAppService.MasterData.SupplierManagementIService.SupplierManagementDto;
using EMS.system.IEntityAppService.MasterData.UnitMeasureService.UnitMeasureDTOs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace EMS.system.EntityAppService.MasterData.SupplierManagementService
{
    public class SupplierManagementAppServices : ApplicationService, ISupplierManagementAppServices
    {
        private readonly IRepository<SupplierManagement> _repository;

        public SupplierManagementAppServices(IRepository<SupplierManagement> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> InsertSupplierManagement(SelectSupplierManagementDto dto)
        {
            SupplierManagement supplierManagement = await _repository.InsertAsync(new SupplierManagement
            {
                SupplierManagementCode = dto.SupplierManagementCode,
                SupplierName = dto.SupplierName,
                SupplierAbbreviation = dto.SupplierAbbreviation,
                SupplierGrade = dto.SupplierGrade,
                SupplierScore = dto.SupplierScore,
                SupplierPhone = dto.SupplierPhone,
                State = dto.State,
            });
            return supplierManagement == null ? 0 : supplierManagement.Id;
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="SupplierManagementCode"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        //public async Task<PagedResult> GetSupplierManagement(string? SupplierManagementCode, string? SupplierName, string? SupplierAbbreviation, bool State, int page, int limit)
        //{
        //    var dto = await _repository.GetListAsync(x => string.IsNullOrEmpty(SupplierManagementCode) || x.SupplierManagementCode.Contains(SupplierManagementCode));
        //    PagedResult pagedResult = new PagedResult();
        //    pagedResult.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / limit);
        //    pagedResult.PageSize = limit;
        //    pagedResult.CurrentPage = page;
        //    pagedResult.RowCount = dto.Count;
        //    pagedResult.Queryable = dto.Skip((page - 1) * limit).Take(limit).AsQueryable();
        //    return pagedResult;
        //}
        public (List<SelectSupplierManagementDto> units, int total) GetSupplierManagement(string? SupplierManagementCode, int pageIndex, int pageSize)
        {
            var entity = _repository.GetListAsync().Result;
            var list = entity.Select(x => new SelectSupplierManagementDto
            {
                Id = x.Id,
                SupplierManagementCode = x.SupplierManagementCode,
                SupplierName = x.SupplierName,
                SupplierAbbreviation = x.SupplierAbbreviation,
                SupplierGrade = x.SupplierGrade,
                SupplierScore = x.SupplierScore,
                SupplierPhone = x.SupplierPhone,
                State = x.State,
            }).ToList();
            if (!string.IsNullOrEmpty(SupplierManagementCode))
            {
                list = list.Where(x => x.SupplierManagementCode.Contains(SupplierManagementCode)).ToList();
            }
            int total = list.Count;
            var pageCount = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return (pageCount, total);
        }
        /// <summary>
        /// 修改反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SelectSupplierManagementDto> GetUpdateSupplierManagement(int id)
        {
            var selectSupplierManagement = await _repository.FirstOrDefaultAsync(x => x.Id == id);
            var result = ObjectMapper.Map<SupplierManagement,SelectSupplierManagementDto>(selectSupplierManagement);
            return result;
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SelectSupplierManagementDto> GetUpdate(int id)
        {
            var entity = _repository.GetListAsync().Result.FirstOrDefault(x => x.Id == id);
            return new SelectSupplierManagementDto()
            {
                
                SupplierManagementCode = entity.SupplierManagementCode,
                SupplierName = entity.SupplierName,
                SupplierAbbreviation = entity.SupplierAbbreviation,
                SupplierGrade = entity.SupplierGrade,
                SupplierScore = entity.SupplierScore,
                SupplierPhone = entity.SupplierPhone,
                State = entity.State,
            };
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> UpdateSupplierManagement(SelectSupplierManagementDto dto)
        {
            var entity = _repository.GetListAsync().Result.FirstOrDefault(x => x.Id == dto.Id);
            entity.SupplierManagementCode = dto.SupplierManagementCode;
            entity.SupplierName = dto.SupplierName;
            entity.SupplierAbbreviation = dto.SupplierAbbreviation;
            entity.SupplierGrade = dto.SupplierGrade;
            entity.SupplierScore = dto.SupplierScore;
            entity.SupplierPhone = dto.SupplierPhone;
            entity.State = dto.State;
            var supplier = await _repository.UpdateAsync(entity);
            return supplier == null ? 0 : supplier.Id;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteSupplierManagement(int id)
        {
            await _repository.DeleteAsync(x => x.Id == id);
            return 1;
        }

    };


}
